//
//  ViewController.m
//  20502
//
//  Created by student on 16/3/22.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextView *textview;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *juhua;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
-(NSString *)dosomething1
{
    [NSThread sleepForTimeInterval:1];
    return @"从网上获取一段信息abcdefg";
}
-(NSString *)dosomething2:(NSString *)str
{
    [NSThread sleepForTimeInterval:2];
    return [str uppercaseString];
}
-(NSString *)dosomething3:(NSString *)str
{
    [NSThread sleepForTimeInterval:3];
    return [NSString stringWithFormat:@"%lu",str.length];
}
-(NSString *)dosomething4:(NSString *)str
{
    [NSThread sleepForTimeInterval:4];
    return [str stringByReplacingOccurrencesOfString:@"a" withString:@"A"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)pressed1:(id)sender
{
    NSDate *d1=[NSDate date];
    NSString *str1=[self dosomething1];
    NSString *str2=[self dosomething2:str1];
    NSString *str3=[self dosomething3:str1];
    NSString *str4=[self dosomething4:str1];
    
    self.textview.text=[NSString stringWithFormat:@"%@\n%@\n%@\n%@",str1,str2,str3,str4];
    [self.juhua stopAnimating];
    NSDate *d2=[NSDate date];
    NSTimeInterval inter=[d2 timeIntervalSinceDate:d1];
    NSLog(@"%.3f",inter);
}
- (IBAction)pressed2:(id)sender
{
    [self.juhua startAnimating];
    dispatch_queue_t queue=dispatch_queue_create(NULL, NULL);
    dispatch_async(queue, ^{
         NSLog(@"当前在:%@",[NSThread currentThread].isMainThread?@"主线程中":@"子线程中");
        NSString *str1=[self dosomething1];
        NSString *str2=[self dosomething2:str1];
        NSString *str3=[self dosomething3:str1];
        NSString *str4=[self dosomething4:str1];
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"当前在:%@",[NSThread currentThread].isMainThread?@"主线程中":@"子线程中");
            self.textview.text=[NSString stringWithFormat:@"%@\n%@\n%@\n%@",str1,str2,str3,str4];
            [self.juhua stopAnimating];

        });

    });
}
- (IBAction)pressed3:(id)sender
{
    [self.juhua startAnimating];
    dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        NSString *str1=[self dosomething1];
        dispatch_group_t group=dispatch_group_create();
        __block NSString *str2;
        __block NSString *str3;
        __block NSString *str4;
         dispatch_group_async(group, queue, ^{
            str2=[self dosomething2:str1];
        });
        dispatch_group_async(group, queue, ^{
            str3=[self dosomething3:str1];
        });
        dispatch_group_async(group, queue, ^{
            str4=[self dosomething4:str1];
        });
        dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
        dispatch_async(dispatch_get_main_queue(), ^{
            self.textview.text = [NSString stringWithFormat:@"%@\n%@\n%@\n%@",str1,str2,str3,str4];
            [self.juhua stopAnimating];
        });
    });

}
- (IBAction)pressed5:(id)sender
{
    [self.juhua startAnimating];
    [self performSelectorInBackground:@selector(calculate) withObject:nil];
}
- (void)calculate
{
    NSString *str1 = [self dosomething1];// 1
    NSString *str2 = [self dosomething2:str1];// 2
    NSString *str3 = [self dosomething3:str1];// 3
    NSString *str4 = [self dosomething4:str1];// 4
    NSString *result = [NSString stringWithFormat:@"%@\n%@\n%@\n%@",str1,str2,str3,str4];
    [self performSelectorOnMainThread:@selector(showresult:) withObject:result waitUntilDone:NO];
}
-(void)showresult:(NSString *)result
{
    self.textview.text=result;
    [self.juhua stopAnimating];
}
- (IBAction)pressed4:(id)sender
{
    [self.juhua startAnimating];
    dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        NSString *str1=[self dosomething1];
        dispatch_group_t group=dispatch_group_create();
        __block NSString *str2;
        __block NSString *str3;
        __block NSString *str4;
        dispatch_group_async(group, queue, ^{
            str2=[self dosomething2:str1];
        });
        dispatch_group_async(group, queue, ^{
            str3=[self dosomething2:str1];
        });
        dispatch_group_async(group, queue, ^{
            str4=[self dosomething2:str1];
        });
        dispatch_group_notify(group, queue, ^{
            dispatch_async(dispatch_get_main_queue(), ^{
                self.textview.text = [NSString stringWithFormat:@"%@\n%@\n%@\n%@",str1,str2,str3,str4];
                [self.juhua stopAnimating];
            });
            

        });

    });
}
                   
@end
